const mysql = require('mysql')
const bcrypt = require('bcryptjs')
const moment = require('moment')

const db = mysql.createPool({
    host:'127.0.0.1',
    user:'root',
    password:'admin123',
    database:'my_db_02'
})

//如果一个分类都没有则默认创建一个
db.query('select count(*) as total from reimbursement_types ',(err,result)=>{
    if(err) return false
    if(result[0].total===0){
        db.query('insert into reimbursement_types (type) values ("笔记本")')
    }
})

//创建默认管理员账户
db.query('select *  from users where level=1 ',(err,result)=>{
    if(err) return false
    if(result.length===0){
        db.query(`insert into users set?`,{username:'adminX',password:bcrypt.hashSync('aa123456',10),create_time:moment().format('YYYY-MM-DD HH:mm:ss'),level:1})
    }
})


module.exports = db
